
import { constantRoutes, asyncRoutes } from '@/router'
const state = {
  routes: [constantRoutes]
}
const mutations = {
  setRoutes(state, otherRoutes) {
    // 路由规则不是累加 基于静态路由去添加用户对应的动他路由
    state.routes = [...constantRoutes, ...otherRoutes]
  }
}
function filterMenus(menus, routes) {
  // console.log(menus, routes, 'menus, routes')
  const routeList = []
  menus.forEach(menu => {
    const route = routes.find(d => d.name === menu.menuUrl)
    if (route !== undefined) {
      if (route.children !== undefined) {
        const child = filterMenus(menu.child, route.children)
        route.children = child
      }
      routeList.push(route)
    }
  })
  return routeList
}
const actions = {
  filterRoutes(context, menus) {
    // console.log(asyncRoutes, 'asyncRoutes')
    const validRoutes = filterMenus(menus, asyncRoutes)
    context.commit('setRoutes', validRoutes)
    return validRoutes
    //   var validRoutes = []
    //   menus.forEach(menu => {
    //     const route = asyncRoutes.find(d => d.name === menu.menuUrl)
    //     const child = []
    //     if (route !== undefined) {
    //       if (route.children !== undefined) {
    //         menu.child.forEach(childMenu => {
    //           const childroute = route.children.find(d => d.name === childMenu.menuUrl)
    //           if (childroute !== undefined) {
    //             child.push(childroute)
    //           }
    //         })
    //         // console.log(route, 'route');
    //         route.children = child
    //       }
    //       validRoutes.push(route)
    //     }
    //   })
    // context.commit('setRoutes', validRoutes)
    // return validRoutes
  /* // 过滤出我能看到的路由
    // asyncRoutes menus(能显示的路由)
    // console.log(asyncRoutes, '所有能显示的权限路由')
    // console.log(menus, '拿到的用户可以拥有的菜单权限')
    const menuschild = menus.map(item => item.child)
    var menuschilds = []
    menuschilds = [].concat(...menuschild) // 降维
    // console.log(menuschilds, '拿到的用户可以拥有的菜单子权限')
    const trymenuschild = []
    trymenuschild.push(menuschilds.map(item => item.menuUrl))
    var newmenuschild = []
    newmenuschild = [].concat(...trymenuschild) // 降维
    // console.log(newmenuschild, '拿到的用户可以拥有的菜单子权限名字')
    let otherRoutes = []
    // for (const task of asyncRoutes) {
    //   otherRoutes.push(...menus.filter(item => item.menuUrl === task.name))
    // }
    const has = []
    for (const task of menus) {
      // console.log(task, 88)
      // console.log(otherRoutes, 70)
      // asyncRoutes.forEach(item => {
      //   if (item.menuUrl === task.name) {
      //     otherRoutes.push(item)
      //   }
      // })
      has.push(task.menuUrl)
    }
    // console.log(has, 123)
    // console.log(asyncRoutes, 'asyncRoutes')
    asyncRoutes.sort(function(a, b) {
      // console.log(a.name, 'a', b.name, 'b')
      return a.name.localeCompare(b.name)
    })
    // console.log(asyncRoutes, 'asyncRoutes2')
    otherRoutes = asyncRoutes.filter(item => has.includes(item.name))

    // console.log(asyncRoutes, 'asyncRoutes')
    // console.log(otherRoutes, 11)
    if (otherRoutes.some(item => item.children.length > 0)) {
      otherRoutes.forEach(item => {
        // console.log(item)
        item.children = item.children.filter(value => {
          return newmenuschild.includes(value.name)
          // console.log(newmenuschild.includes(value.name))
          // console.log(value.name)
        })
        // console.log(item.children, '过滤子路由后的', newmenuschild)
      })
      // console.log(otherRoutes, '过滤子路由后的')
    }
    context.commit('setRoutes', otherRoutes)
    return otherRoutes */
  }
}

const getters = {}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
